package org.crystalrange.documents.web;

import java.util.ArrayList;
import java.util.List;

import org.crystalrange.documents.model.Document;
import org.crystalrange.documents.model.DocumentVersion;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Messagebox;

public class DocumentVersionSelectController extends GenericForwardComposer {
	
	private Document document;
	protected DocumentVersion selectedDocumentVersion;
	protected List<DocumentVersion> documentVersions;
	
	public Document getDocument() {
		return document;
	}

	public void setDocument(Document document) {
		this.document = document;
	}

	public DocumentVersion getSelectedDocumentVersion() {
		return selectedDocumentVersion;
	}

	public void setSelectedDocumentVersion(DocumentVersion selectedDocumentVersion) {
		this.selectedDocumentVersion = selectedDocumentVersion;
	}

	public List<DocumentVersion> getDocumentVersions() {
		return documentVersions;
	}

	public void setDocumentVersions(List<DocumentVersion> documentVersions) {
		this.documentVersions = documentVersions;
	}

	@Override
	public void doAfterCompose(Component component) throws Exception {
		super.doAfterCompose(component);
		this.document = (Document) this.execution.getAttribute("document");
		this.documentVersions = new ArrayList<DocumentVersion>();
		documentVersions = this.document.getDocumentVersions();
		component.setAttribute("controller", this);
	}
	
	public void onClick$buttonOk(Event event) throws Exception {
		if (this.selectedDocumentVersion != null) {
			this.execution.setAttribute("selectedDocumentVersion",
					this.selectedDocumentVersion);
			this.execution.setAttribute("result", "ok");
			this.self.detach();
		} else {
			Messagebox.show("Please select a Document Version.");
		}
	}
	
	public void onClick$buttonCancel(Event event) throws Exception {
		this.execution.setAttribute("selectedDocumentVersion", null);
		this.execution.setAttribute("result", "cancel");
		this.self.detach();
	}
	
}
